/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.controller;

import com.minierp.bean.OutBillBean;
import com.minierp.bean.OutBillItemBean;
import com.minierp.bean.UserBean;
import com.minierp.dao.BusinessBillDao;
import com.minierp.dao.BusinessBillItemDao;
import com.minierp.dao.CodeRuleDao;
import com.minierp.dao.IdGenerator;
import com.minierp.dao.InBillItemDao;
import com.minierp.dao.OutBillDao;
import com.minierp.dao.OutBillItemDao;
import com.minierp.dao.OutTypeDao;
import com.minierp.dao.ProductDao;
import com.minierp.dao.PurchaseBillItemDao;
import com.minierp.dao.SerialNumberDao;
import com.minierp.dao.StockBalanceDao;
import com.minierp.dao.StockDao;
import com.minierp.dao.StorehouseDao;
import com.minierp.err.PermissionDennyException;
import com.minierp.err.StockNoEnoughException;
import com.minierp.helper.EncCommonHelper;
import com.minierp.helper.EncOutBillFormCtrHelper;
import com.minierp.helper.PageHelper;
import com.minierp.model.BusinessBill;
import com.minierp.model.CodeRule;
import com.minierp.model.InBill;
import com.minierp.model.InBillItem;
import com.minierp.model.OutBill;
import com.minierp.model.OutBillItem;
import com.minierp.model.Product;
import com.minierp.model.PurchaseBill;
import com.minierp.model.PurchaseBillItem;
import com.minierp.model.Storehouse;
import com.minierp.model.TallySet;
import com.minierp.model.WorkCenter;
import com.minierp.service.OutBillService;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import org.springframework.dao.CannotAcquireLockException;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.view.RedirectView;

public class OutBillFormCtr extends SimpleFormController {
	private OutTypeDao outTypeDao;
	private OutBillItemDao outBillItemDao;
	private InBillItemDao inBillItemDao;
	private PurchaseBillItemDao purchaseBillItemDao;
	private BusinessBillItemDao businessBillItemDao;
	private OutBillDao outBillDao;
	private CodeRuleDao codeRuleDao;
	private SerialNumberDao serialNumberDao;
	private StorehouseDao storehouseDao;
	private ProductDao productDao;
	private StockDao stockDao;
	private StockBalanceDao stockBalanceDao;
	private BusinessBillDao businessBillDao;
	private IdGenerator idGenerator;
	private OutBillService outBillService;
	private DataSource dataSource;

	protected Map referenceData(HttpServletRequest request) throws Exception {
		HashMap refData = new HashMap();
		String id = request.getParameter("id");
		if (id == null) {
			id = "";
		}

		refData.put("id", id);
		refData.put("outTypeList", this.outTypeDao.getList(false));
		refData.put("storehouseList", this.storehouseDao.getList(false));
		return refData;
	}

	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		String id = request.getParameter("id");
		CodeRule cr = this.codeRuleDao.get("OUT_BILL");
		if (cr == null) {
			throw new Exception("Code rule not found for businessBill");
		} else {
			OutBillBean form;
			if (id != null && !id.equals("")) {
				OutBill arg28 = (OutBill) this.outBillDao.get(OutBill.class,
						Long.parseLong(id));
				if (arg28 == null) {
					throw new Exception("Record not found for id:" + id);
				}

				String requirerName = PageHelper.getRequName(
						arg28.getRequirerId(), arg28.getRequirerType(),
						this.outBillDao);
				TallySet arg29 = (TallySet) this.outBillDao.get(TallySet.class,
						arg28.getTallySetId());
				List arg30 = this.outBillItemDao.getList(arg28.getId());
				long[] arg31 = new long[arg30.size()];

				for (int arg32 = 0; arg32 < arg30.size(); ++arg32) {
					arg31[arg32] = ((OutBillItem) arg30.get(arg32))
							.getProductId();
				}

				List arg33 = this.productDao.getList(arg31);
				form = EncOutBillFormCtrHelper.doFormBackingObjectEdit(cr,
						arg28, requirerName, arg29, arg30, arg33);
				if (arg28.getExamined().equals("Y")) {
					PageHelper.setCostInfo(form.getItems());
				} else {
					PageHelper.setCostInfo(request, form.getItems(),
							this.stockDao, arg28.getStorehouseId());
				}
			} else {
				if (!EncCommonHelper.checkPrivilege(request, "OUT_BILL")) {
					throw new PermissionDennyException("Permission Denny");
				}

				long storehouseId = 0L;
				String bbCode = "";
				String requirerName1 = "";
				String requirerType = "";
				long requirerId = 0L;
				long managerId = 0L;
				String managerName = "";
				TallySet ts = null;
				boolean getDefault = false;
				ArrayList itemList = new ArrayList();
				String dataType = request.getParameter("data_type");
				if (dataType == null) {
					dataType = "";
				}

				String ub;
				List i;
				int purItem;
				if (dataType.equals("COPY")) {
					ub = request.getParameter("template_id");
					OutBill storehouseList = (OutBill) this.outBillDao.get(
							OutBill.class, Long.parseLong(ub));
					if (storehouseList == null) {
						throw new Exception(
								"Template OutBill not found for id " + ub);
					}

					requirerName1 = PageHelper.getRequName(
							storehouseList.getRequirerId(),
							storehouseList.getRequirerType(), this.outBillDao);
					ts = (TallySet) this.outBillDao.get(TallySet.class,
							storehouseList.getTallySetId());
					i = this.outBillItemDao.getList(storehouseList.getId());
					long[] sto = new long[i.size()];

					for (purItem = 0; purItem < i.size(); ++purItem) {
						sto[purItem] = ((OutBillItem) i.get(purItem))
								.getProductId();
					}

					List arg42 = this.productDao.getList(sto);
					form = EncOutBillFormCtrHelper.doFormBackingObjectTemplate(
							request, cr, storehouseList, requirerName1, ts, i,
							arg42);
				} else {
					if (dataType.equals("BUSINESS")) {
						ub = request.getParameter("business_bill_id");
						requirerType = request.getParameter("requirer_type");
						BusinessBill arg37 = (BusinessBill) this.outBillDao
								.get(BusinessBill.class, Long.parseLong(ub));
						if (arg37 != null) {
							ts = (TallySet) this.outBillDao.get(TallySet.class,
									arg37.getTallySetId());
							bbCode = arg37.getBillCode();
						}

						if (requirerType != null
								&& requirerType.equals("WORK_CENTER")) {
							requirerId = Long.parseLong(request
									.getParameter("requirer_id"));
							WorkCenter arg39 = (WorkCenter) this.outBillDao
									.get(WorkCenter.class, requirerId);
							if (arg39 != null) {
								requirerName1 = arg39.getWcName();
								storehouseId = arg39.getStorehouseId();
							}

							List arg43 = this.businessBillItemDao
									.getProductList(Long.parseLong(ub),
											requirerId);

							for (purItem = 0; purItem < arg43.size(); ++purItem) {
								Object[] arg47 = (Object[]) arg43.get(purItem);
								Product arg48 = (Product) arg47[0];
								BigDecimal arg49;
								if (arg47[1].toString().equals("AMOUNT")) {
									arg49 = (BigDecimal) arg47[2];
								} else {
									arg49 = (BigDecimal) arg47[3];
								}

								OutBillItem outItem1 = new OutBillItem();
								OutBillItemBean bean1 = new OutBillItemBean();
								outItem1.setProductId(arg48.getId());
								outItem1.setAmount(arg49);
								bean1.setProductCode(arg48.getProductCode());
								bean1.setProductName(arg48.getProductName());
								bean1.setUnit(arg48.getUnit());
								bean1.setAmountStr(EncCommonHelper
										.parseBigDecimal(outItem1.getAmount()));
								bean1.setOutBillItem(outItem1);
								bean1.setStatus("NEW");
								itemList.add(bean1);
							}
						}
					} else {
						OutBillItem outItem;
						OutBillItemBean bean;
						Product p;
						int arg41;
						if (dataType.equals("IN_BILL")) {
							ub = request.getParameter("in_bill_id");
							InBill arg36 = (InBill) this.outBillDao.get(
									InBill.class, Long.parseLong(ub));
							if (arg36 != null) {
								storehouseId = arg36.getStorehouseId();
								managerId = arg36.getManagerId();
								managerName = arg36.getManagerName();
								ts = (TallySet) this.outBillDao.get(
										TallySet.class, arg36.getTallySetId());
								i = this.inBillItemDao.getList(arg36.getId());

								for (arg41 = 0; arg41 < i.size(); ++arg41) {
									InBillItem arg46 = (InBillItem) i
											.get(arg41);
									outItem = new OutBillItem();
									bean = new OutBillItemBean();
									outItem.setAmount(arg46.getAmount());
									outItem.setProductId(arg46.getProductId());
									bean.setAmountStr(EncCommonHelper
											.parseBigDecimal(outItem
													.getAmount()));
									bean.setOutBillItem(outItem);
									p = (Product) this.outBillDao.get(
											Product.class,
											outItem.getProductId());
									if (p != null) {
										bean.setProductCode(p.getProductCode());
										bean.setProductName(p.getProductName());
										bean.setUnit(p.getUnit());
									}

									bean.setStatus("NEW");
									itemList.add(bean);
								}
							}
						} else if (dataType.equals("PURCHASE")) {
							ub = request.getParameter("purchase_bill_id");
							PurchaseBill arg34 = (PurchaseBill) this.outBillDao
									.get(PurchaseBill.class, Long.parseLong(ub));
							if (arg34 != null) {
								managerId = arg34.getManagerId();
								managerName = arg34.getManagerName();
								ts = (TallySet) this.outBillDao.get(
										TallySet.class, arg34.getTallySetId());
								storehouseId = arg34.getStorehouseId();
								i = this.purchaseBillItemDao.getList(arg34
										.getId());

								for (arg41 = 0; arg41 < i.size(); ++arg41) {
									PurchaseBillItem arg45 = (PurchaseBillItem) i
											.get(arg41);
									outItem = new OutBillItem();
									bean = new OutBillItemBean();
									outItem.setAmount(arg45.getAmount());
									outItem.setProductId(arg45.getProductId());
									bean.setAmountStr(EncCommonHelper
											.parseBigDecimal(outItem
													.getAmount()));
									bean.setOutBillItem(outItem);
									p = (Product) this.outBillDao.get(
											Product.class,
											outItem.getProductId());
									if (p != null) {
										bean.setProductCode(p.getProductCode());
										bean.setProductName(p.getProductName());
										bean.setUnit(p.getUnit());
									}

									bean.setStatus("NEW");
									itemList.add(bean);
								}
							}
						} else {
							getDefault = true;
						}
					}

					if (getDefault) {
						UserBean arg35 = EncCommonHelper
								.getCurrentUserAccount(request);
						List arg38 = this.storehouseDao.getList(arg35);

						for (int arg40 = 0; arg40 < arg38.size(); ++arg40) {
							Storehouse arg44 = (Storehouse) arg38.get(0);
							if (ts != null) {
								if (arg44.getTallySetId() == ts.getId()) {
									storehouseId = arg44.getId();
									break;
								}
							} else if (arg44.getTallySetId() == arg35.getUser()
									.getDfTsId()) {
								storehouseId = arg44.getId();
								break;
							}
						}
					}

					form = EncOutBillFormCtrHelper.doFormBackingObjectNew(
							request, cr, storehouseId, managerId, managerName,
							bbCode, requirerType, requirerId, requirerName1,
							ts, itemList);
				}
			}

			return form;
		}
	}

	protected void onBind(HttpServletRequest request, Object command)
			throws Exception {
		OutBillBean form = (OutBillBean) command;
		EncOutBillFormCtrHelper.doOnBind(request, form, this.dataSource);
	}

	protected void onBindAndValidate(HttpServletRequest request,
			Object command, BindException errors) throws Exception {
	}

	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		ModelAndView mv = new ModelAndView(this.getSuccessView());
		String id = request.getParameter("id");
		String examine = request.getParameter("examine");
		String alertMsg = "";
		OutBillBean form = (OutBillBean) command;
		OutBill obj = form.getOutBill();
		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		if (examine != null && examine.equals("1")) {
			if (!EncCommonHelper.checkPrivilege(request, "OUT_EXAM")) {
				throw new Exception("Permission deny!");
			}

			obj.setExamined("Y");
			if (ub.getStaffer() != null) {
				obj.setExaminerId(ub.getStaffer().getId());
				obj.setExaminerName(ub.getStaffer().getStafferName());
			}
		} else {
			obj.setExamined("N");
		}

		if (id != null && !id.equals("")) {
			if (!EncCommonHelper.checkPrivilege(request, "OUT_BILL")) {
				throw new Exception("Permission deny!");
			}

			try {
				this.outBillService.txUpdate(form, this.outBillDao,
						this.outBillItemDao, this.stockDao,
						this.stockBalanceDao, this.businessBillDao,
						this.idGenerator);
			} catch (StockNoEnoughException arg13) {
				EncCommonHelper.setAlertMessage(request, arg13.getMessage());
				return this.showForm(request, errors, this.getFormView());
			} catch (CannotAcquireLockException arg14) {
				EncCommonHelper.setAlertMessage(request, "事务冲突故障，请重新操作！");
				return this.showForm(request, errors, this.getFormView());
			}

			alertMsg = "修改成功！";
		} else {
			try {
				this.outBillService.txInsert(form, this.outBillDao,
						this.outBillItemDao, this.stockDao,
						this.stockBalanceDao, this.serialNumberDao,
						this.businessBillDao, this.idGenerator);
			} catch (StockNoEnoughException arg15) {
				EncCommonHelper.setAlertMessage(request, arg15.getMessage());
				return this.showForm(request, errors, this.getFormView());
			} catch (CannotAcquireLockException arg16) {
				EncCommonHelper.setAlertMessage(request, "事务冲突故障，请重新操作！");
				return this.showForm(request, errors, this.getFormView());
			}

			alertMsg = "增加成功！";
		}

		mv.addObject("returnValue", String.valueOf(obj.getId()));
		String continueFlag = request.getParameter("continue_flag");
		String alertType = request.getParameter("alert_type");
		if (continueFlag != null && continueFlag.equals("1")) {
			EncCommonHelper.setAlertMessage(request, alertMsg);
			mv = new ModelAndView(
					"redirect:/storehouse/OutBillNew.ihtm?alert_type="
							+ alertType);
		} else if (continueFlag != null && continueFlag.equals("2")) {
			EncCommonHelper.setAlertMessage(request, alertMsg);
			mv = new ModelAndView(
					"redirect:/storehouse/OutBillNew.ihtm?alert_type="
							+ alertType + "&data_type=COPY&template_id="
							+ obj.getId());
		} else if (alertType == null || !alertType.equals("NONE")) {
			EncCommonHelper.setAlertMessage(request, alertMsg);
			if (alertType != null && alertType.equals("FORM")) {
				mv = new ModelAndView(new RedirectView(
						"/storehouse/OutBillEdit.ihtm?id=" + obj.getId()
								+ "&alert_type=" + alertType));
			}
		}

		mv.addObject("alertType", alertType);
		return mv;
	}

	public IdGenerator getIdGenerator() {
		return this.idGenerator;
	}

	public void setIdGenerator(IdGenerator idGenerator) {
		this.idGenerator = idGenerator;
	}

	public CodeRuleDao getCodeRuleDao() {
		return this.codeRuleDao;
	}

	public void setCodeRuleDao(CodeRuleDao codeRuleDao) {
		this.codeRuleDao = codeRuleDao;
	}

	public SerialNumberDao getSerialNumberDao() {
		return this.serialNumberDao;
	}

	public void setSerialNumberDao(SerialNumberDao serialNumberDao) {
		this.serialNumberDao = serialNumberDao;
	}

	public ProductDao getProductDao() {
		return this.productDao;
	}

	public void setProductDao(ProductDao productDao) {
		this.productDao = productDao;
	}

	public DataSource getDataSource() {
		return this.dataSource;
	}

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	public StorehouseDao getStorehouseDao() {
		return this.storehouseDao;
	}

	public void setStorehouseDao(StorehouseDao storehouseDao) {
		this.storehouseDao = storehouseDao;
	}

	public StockDao getStockDao() {
		return this.stockDao;
	}

	public void setStockDao(StockDao stockDao) {
		this.stockDao = stockDao;
	}

	public OutBillDao getOutBillDao() {
		return this.outBillDao;
	}

	public void setOutBillDao(OutBillDao outBillDao) {
		this.outBillDao = outBillDao;
	}

	public OutBillItemDao getOutBillItemDao() {
		return this.outBillItemDao;
	}

	public void setOutBillItemDao(OutBillItemDao outBillItemDao) {
		this.outBillItemDao = outBillItemDao;
	}

	public OutBillService getOutBillService() {
		return this.outBillService;
	}

	public void setOutBillService(OutBillService outBillService) {
		this.outBillService = outBillService;
	}

	public OutTypeDao getOutTypeDao() {
		return this.outTypeDao;
	}

	public void setOutTypeDao(OutTypeDao outTypeDao) {
		this.outTypeDao = outTypeDao;
	}

	public StockBalanceDao getStockBalanceDao() {
		return this.stockBalanceDao;
	}

	public void setStockBalanceDao(StockBalanceDao stockBalanceDao) {
		this.stockBalanceDao = stockBalanceDao;
	}

	public BusinessBillDao getBusinessBillDao() {
		return this.businessBillDao;
	}

	public void setBusinessBillDao(BusinessBillDao businessBillDao) {
		this.businessBillDao = businessBillDao;
	}

	public InBillItemDao getInBillItemDao() {
		return this.inBillItemDao;
	}

	public void setInBillItemDao(InBillItemDao inBillItemDao) {
		this.inBillItemDao = inBillItemDao;
	}

	public PurchaseBillItemDao getPurchaseBillItemDao() {
		return this.purchaseBillItemDao;
	}

	public void setPurchaseBillItemDao(PurchaseBillItemDao purchaseBillItemDao) {
		this.purchaseBillItemDao = purchaseBillItemDao;
	}

	public BusinessBillItemDao getBusinessBillItemDao() {
		return this.businessBillItemDao;
	}

	public void setBusinessBillItemDao(BusinessBillItemDao businessBillItemDao) {
		this.businessBillItemDao = businessBillItemDao;
	}
}